﻿using PBO.Data;
using PBO.Data.Domain;
using PBO.Game.BattleLog;
using PBO.Network;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace PBO.Game.BattleClass
{
    public partial class Pokemon
    {
        private Item mItem;

        public Item GetItem(bool ignoreInvalid = false)
        {
            if (MyTeam.ground.MagicRoom)
            {
                return Item.None;
            }
            if ((BattleState.detain || GetSelTrait() == Trait.Klutz) && !ignoreInvalid)
                return Item.None;
            return mItem;
        }

        public void SetThisItem(Item value)
        {
            mItem = value;
            if (GetSelTrait(true) == Trait.Unburden)
            {
                if (value == Item.None)
                {
                    BattleState.traitRaised = true;
                }
                else
                {
                    BattleState.traitRaised = false;
                }
            }
        }

        public bool ChoiceItem
        {
            get { return GetItem().IsChoiceItem(); }
        }

        public Item UsedItem { get; set; }

        public void UseItem()
        {
            this.UsedItem = GetItem();
            SetItem(Item.None);
        }

        public void RaiseItem()
        {
            var user_item = GetItem();
            if (user_item.IsHalfHurtBerry())
            {
                MyTeam.log.AppendMsg("Antiberry", user_item, this.GetNameString());
                UseItem();
                return;
            }
            switch (user_item)
            {
                case Item.CheriBerry:
                    if (State == PokemonState.Paralysis)
                    {
                        State = PokemonState.No;
                        MyTeam.log.AppendMsg("ItemDePAR", this.GetNameString(), Item.CheriBerry);
                        //MyTeam.log.AddText(GetNameString() + "的解麻果使它解除了麻痹状态");
                        UseItem();
                    }
                    break;
                case Item.ChestoBerry:
                    if (State == PokemonState.Sleep)
                    {
                        State = PokemonState.No;
                        MyTeam.log.AppendMsg("ItemDeSLP", this.GetNameString(), Item.ChestoBerry);
                        //MyTeam.log.AddText(GetNameString() + "的睡眠果使它解除了睡眠状态");
                        UseItem();
                    }
                    break;
                case Item.PechaBerry:
                    if (State == PokemonState.Toxin || State == PokemonState.Poison)
                    {
                        State = PokemonState.No;
                        MyTeam.log.AppendMsg("ItemDePSN", this.GetNameString(), Item.PechaBerry);
                        //MyTeam.log.AddText(GetNameString() + "的解毒果使它解除了中毒状态");
                        UseItem();
                    }
                    break;
                case Item.RawstBerry:
                    if (State == PokemonState.Burn)
                    {
                        State = PokemonState.No;
                        MyTeam.log.AppendMsg("ItemDeBRN", this.GetNameString(), Item.RawstBerry);
                        //MyTeam.log.AddText(GetNameString() + "的烧伤果使它解除了烧伤状态");
                        UseItem();
                    }
                    break;
                case Item.AspearBerry:
                    if (State == PokemonState.Freeze)
                    {
                        State = PokemonState.No;
                        MyTeam.log.AppendMsg("ItemDeFRZ", this.GetNameString(), Item.AspearBerry);
                        //MyTeam.log.AddText(GetNameString() + "的解冻果使它解除了冻结状态");
                        UseItem();
                    }
                    break;
                case Item.LeppaBerry:
                    var move = this.GetSelMoves().FirstOrDefault(m => m.PP == 0);
                    if (move != null)
                    {
                        move.PP += 10;
                        MyTeam.log.AppendMsg("ItemPPRecover", this.GetNameString(), Item.OranBerry, move.Data.Name);
                        //MyTeam.log.AddText(GetNameString() + "的6号果实使它的技能回复了PP");
                        UseItem();
                        return;
                    }
                    break;
                case Item.OranBerry:
                    if (HealthPower <= MaxHp * 0.5)
                    {
                        MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.OranBerry);
                        //MyTeam.log.AddText(GetNameString() + "的7号果实使它回复了HP！");
                        HPRecover(10);
                        UseItem();
                    }
                    break;
                case Item.PersimBerry:
                    if (BattleState.confusion)
                    {
                        BattleState.confusion = false;
                        MyTeam.log.AppendMsg("ItemDeConfuse", this.GetNameString(), Item.PersimBerry);
                        //MyTeam.log.AddText(GetNameString() + "的混乱果使它解除了混乱状态！");
                        UseItem();
                    }
                    break;
                case Item.LumBerry:
                    if (BattleState.confusion)
                    {
                        BattleState.confusion = false;
                        MyTeam.log.AppendMsg("ItemDeConfuse", this.GetNameString(), Item.LumBerry);
                        //MyTeam.log.AppendText(GetNameString() + "的奇迹果使它解除了混乱状态！");
                        UseItem();
                    }
                    else if (State != PokemonState.No)
                    {
                        State = PokemonState.No;
                        MyTeam.log.AppendText(GetNameString() + "的奇迹果使它恢复了状态！");
                        UseItem();
                    }
                    break;
                case Item.SitrusBerry:
                    if (HealthPower <= MaxHp * 0.5)
                    {
                        MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.SitrusBerry);
                        //MyTeam.log.AddText(GetNameString() + "的10号果实使它回复了HP！");
                        this.HPRecoverFraction(4);
                        UseItem();
                    }
                    break;
                case Item.FigyBerry:
                    if (HealthPower <= MaxHp * 0.5)
                    {
                        MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.FigyBerry);
                        //MyTeam.log.AddText(GetNameString() + "的11号果实使它回复了HP！");
                        this.HPRecoverFraction(4);
                        if (((int)Nature + 1) % 5 == 1 && (int)Nature != 0)
                            confuse(false);
                        UseItem();
                    }
                    break;
                case Item.WikiBerry:
                    if (HealthPower <= MaxHp * 0.5)
                    {
                        MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.WikiBerry);
                        //MyTeam.log.AddText(GetNameString() + "的12号果实使它回复了HP！");
                        this.HPRecoverFraction(8);
                        if (((int)Nature + 1) % 5 == 2 && (int)Nature != 6)
                            confuse(false);
                        UseItem();
                    }
                    break;
                case Item.MagoBerry:
                    if (HealthPower <= MaxHp * 0.5)
                    {
                        MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.MagoBerry);
                        //MyTeam.log.AddText(GetNameString() + "的13号果实使它回复了HP！");
                        this.HPRecoverFraction(8);
                        if (((int)Nature + 1) % 5 == 3 && (int)Nature != 12)
                            confuse(false);
                        UseItem();
                    }
                    break;
                case Item.AguavBerry:
                    if (HealthPower <= MaxHp * 0.5)
                    {
                        MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.AguavBerry);
                        //MyTeam.log.AddText(GetNameString() + "的14号果实使它回复了HP！");
                        this.HPRecoverFraction(8);
                        if (((int)Nature + 1) % 5 == 4 && (int)Nature != 18)
                            confuse(false);
                        UseItem();
                    }
                    break;
                case Item.IapapaBerry:
                    if (HealthPower <= MaxHp * 0.5)
                    {
                        MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.IapapaBerry);
                        //MyTeam.log.AddText(GetNameString() + "的15号果实使它回复了HP！");
                        this.HPRecoverFraction(8);
                        if (((int)Nature + 1) % 5 == 5 && (int)Nature != 24)
                            confuse(false);
                        UseItem();
                    }

                    break;
                case Item.LiechiBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("Item7DUp1", this.GetNameString(), Item.LiechiBerry, StatType.Atk);
                        //MyTeam.log.AppendText(GetNameString() + "的物攻果发动了！");
                        AbilityLvUpChanged(StatType.Atk, 1, false);
                        UseItem();
                    }
                    break;
                case Item.GanlonBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("Item7DUp1", this.GetNameString(), Item.GanlonBerry, StatType.Def);
                        //MyTeam.log.AppendText(GetNameString() + "的物防果发动了！");
                        AbilityLvUpChanged(StatType.Def, 1, false);
                        UseItem();
                    }
                    break;
                case Item.SalacBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("Item7DUp1", this.GetNameString(), Item.SalacBerry, StatType.Speed);
                        //MyTeam.log.AppendText(GetNameString() + "的速度果发动了！");
                        AbilityLvUpChanged(StatType.Speed, 1, false);
                        UseItem();
                    }
                    break;
                case Item.PetayaBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("Item7DUp1", this.GetNameString(), Item.PetayaBerry, StatType.SpAtk);
                        //MyTeam.log.AppendText(GetNameString() + "的特攻果发动了！");
                        AbilityLvUpChanged(StatType.SpAtk, 1, false);
                        UseItem();
                    }
                    break;
                case Item.ApicotBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("Item7DUp1", this.GetNameString(), Item.ApicotBerry, StatType.SpDef);
                        //MyTeam.log.AppendText(GetNameString() + "的特防果发动了！");
                        AbilityLvUpChanged(StatType.SpDef, 1, false);
                        UseItem();
                    }
                    break;
                case Item.LansatBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("ItemEnFocusEnergy", this.GetNameString(), Item.LansatBerry);
                        //MyTeam.log.AppendText(GetNameString() + "的会心果发动了！");
                        if (!BattleState.FocusEngery)
                            CTLVUp(1);
                        UseItem();
                    }
                    break;
                case Item.StarfBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        var ss = from s in BattleStats.Type5D
                                 where this.BattleState.AbilityLv[s] < 6
                                 select s;
                        int n = ss.Count();
                        if (n > 0)
                        {
                            int rnd = MyTeam.random.Next(n);
                            var stat = ss.ElementAt(rnd);
                            MyTeam.log.AppendMsg("Item7DUp2", this.GetNameString(), user_item, stat);
                            //MyTeam.log.AppendText(GetNameString() + "的随机果发动了！");
                            AbilityLvUpChanged(stat, 2, false);
                            UseItem();
                        }
                    }
                    break;
                case Item.MicleBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("MicleBerry", this.GetNameString(), Item.MicleBerry);
                        //MyTeam.log.AppendText(GetNameString() + "的命中果发动了！");
                        AbilityLvUpChanged(StatType.Accuracy, 1, false);
                        //AccuracyLVUp(1, false);
                        UseItem();
                    }
                    break;
                case Item.CustapBerry:
                    if (HealthPower <= MaxHp * 0.25 || (GetSelTrait() == Trait.Gluttony && HealthPower <= MaxHp * 0.5))
                    {
                        MyTeam.log.AppendMsg("QuickItem", this.GetNameString(), Item.CustapBerry);
                        //MyTeam.log.AddText(GetNameString() + "的先制果发动了！");
                        BattleState.quickBerry = true;
                        UseItem();
                    }
                    break;
                case Item.MentalHerb:
                    if (BattleState.captivated || BattleState.encore || BattleState.taunt)
                    {
                        BattleState.captivated = false;
                        BattleState.captivateTarget = null;
                        BattleState.encore = false;
                        BattleState.taunt = false;
                        MyTeam.log.AppendText(GetNameString() + "的精神香草使它回复了状态！");
                        UseItem();
                    }
                    break;
                case Item.WhiteHerb:
                    if (this.BattleState.AbilityLv.Any(v => v < 0))
                    {
                        foreach (var type in BattleStats.Type7D)
                        {
                            if (this.BattleState.AbilityLv[type] < 0)
                            {
                                this.BattleState.AbilityLv[type] = 0;
                            }
                        }
                        MyTeam.log.AppendMsg("WhiteHerb", this.GetNameString(), Item.WhiteHerb);
                        //MyTeam.log.AddText(GetNameString() + "的白色香草使它恢复了能力等级！");
                        UseItem();
                    }
                    break;
                case Item.PowerHerb:
                    MyTeam.log.AppendMsg("PowerHerb", this.GetNameString(), Item.PowerHerb);
                    //MyTeam.log.AddText(GetNameString() + "的力量草药发动了！");
                    BattleState.prepare = false;
                    UseItem();
                    break;
                case Item.EnigmaBerry:
                    this.HPRecoverFraction(4);
                    MyTeam.log.AppendMsg("ItemRecover", this.GetNameString(), Item.EnigmaBerry);
                    //MyTeam.log.AddText(GetNameString() + "的60号果实使它回复了HP！");
                    UseItem();
                    break;
            }
        }

        public void SetItem(Item itemValue)
        {
            if (this.UsedItem.IsChoiceItem())
            {
                this.UsedItem = Item.None;
                if (itemValue.IsChoiceItem())
                {
                    this.UsedItem = itemValue;
                }
            }

            if (itemValue != Item.None)
            {
                MyTeam.log.AppendMsg("GetItem", this.GetNameString(), itemValue);
                //MyTeam.log.AddText(GetNameString() + "得到了" + itemValue.ToString() + "！");
            }

            SetThisItem(itemValue);
            if (GetItem(true).IsRaiseItem())
            {
                RaiseItem();
            }

            foreach (var move in this.GetSelMoves())
            {
                //MoveAdditionalEffect.科技爆破
                if (move.Data.AddEffect2 == MoveAdditionalEffect.制裁飞石 || move.Data.AddEffect2 == MoveAdditionalEffect.自然恩惠)
                {
                    UpdateMove(move);
                }
            }

            if (GetSelTrait() == Trait.Multitype) RaiseTrait();
        }
    }
}
